/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import ModeloLogicoSistema.AdministradorServicio;
import ModeloLogicoSistema.Servicio;
import ModeloLogicoSistema.Servicio.TipoServicio;
import ModeloLogicoSistema.Validacion;
import javax.swing.JOptionPane;

/**
 *
 * @author Veko
 */
public class ModificacionServicio extends javax.swing.JDialog {

    /**
     * Creates new form ModificacionServicio
     */
    
    Servicio servicio;
        
    public ModificacionServicio(java.awt.Frame parent, boolean modal, Servicio servicio) {
        super(parent, modal);
        initComponents();

        this.setLocationRelativeTo(null);
        this.servicio=servicio;
        cargarServicio();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelPrecioError1 = new javax.swing.JLabel();
        buttonGroup1 = new javax.swing.ButtonGroup();
        BtnLimpiar = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        cbTipoServicio = new javax.swing.JComboBox();
        BtnAceptar = new javax.swing.JButton();
        RBter = new javax.swing.JRadioButton();
        labelPrecioError = new javax.swing.JLabel();
        precioT = new javax.swing.JTextField();
        labelLocalTercError = new javax.swing.JLabel();
        txtPrecio = new javax.swing.JTextField();
        labelPrecioTercError = new javax.swing.JLabel();
        RBlocal = new javax.swing.JRadioButton();
        txtIdServicio = new javax.swing.JTextField();
        txtNombre = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        BtnCerrar = new javax.swing.JButton();
        labelPrecio = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        labelPrecioT = new javax.swing.JLabel();
        labelNombreError = new javax.swing.JLabel();
        jLabelPrecioT = new javax.swing.JLabel();

        labelPrecioError1.setForeground(new java.awt.Color(255, 0, 0));
        labelPrecioError1.setText("Campo Obligatorio");
        labelPrecioError.setVisible(false);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Modificar Servicio");

        BtnLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/limpiar.png"))); // NOI18N
        BtnLimpiar.setText("Limpiar");
        BtnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnLimpiarActionPerformed(evt);
            }
        });

        cbTipoServicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbTipoServicioActionPerformed(evt);
            }
        });

        BtnAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Guardar.png"))); // NOI18N
        BtnAceptar.setText("Aceptar");
        BtnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnAceptarActionPerformed(evt);
            }
        });

        buttonGroup1.add(RBter);
        RBter.setText("Terciarizado");
        RBter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RBterActionPerformed(evt);
            }
        });

        labelPrecioError.setForeground(new java.awt.Color(255, 0, 0));
        labelPrecioError.setText("Verificar");
        labelPrecioError.setVisible(false);

        precioT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                precioTActionPerformed(evt);
            }
        });

        labelLocalTercError.setForeground(new java.awt.Color(255, 0, 0));
        labelLocalTercError.setText("Selección Obligatoria");
        labelLocalTercError.setVisible(false);

        txtPrecio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPrecioActionPerformed(evt);
            }
        });

        labelPrecioTercError.setForeground(new java.awt.Color(255, 0, 0));
        labelPrecioTercError.setText("Verificar");
        labelPrecioTercError.setVisible(false);

        buttonGroup1.add(RBlocal);
        RBlocal.setText("Local");
        RBlocal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RBlocalActionPerformed(evt);
            }
        });

        txtIdServicio.setEditable(false);
        txtIdServicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIdServicioActionPerformed(evt);
            }
        });

        txtNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNombreActionPerformed(evt);
            }
        });

        jLabel5.setText("Tipo de Servicio");

        BtnCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/cancelar.png"))); // NOI18N
        BtnCerrar.setText("Cerrar");
        BtnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCerrarActionPerformed(evt);
            }
        });

        labelPrecio.setText("Precio");

        jLabel2.setText("Nombre");

        jLabel1.setText("ID Servicio");

        labelPrecioT.setText("Precio");

        labelNombreError.setForeground(new java.awt.Color(255, 0, 0));
        labelNombreError.setText("Verificar");
        labelNombreError.setVisible(false);

        jLabelPrecioT.setText("PrecioT");
        jLabelPrecioT.setFocusable(false);
        jLabelPrecioT.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
                        .addGap(32, 32, 32))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel5)
                                        .addComponent(RBlocal))
                                    .addGap(18, 18, 18)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(labelLocalTercError, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                                        .addComponent(cbTipoServicio, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(RBter)
                                        .addComponent(labelPrecioT, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                                    .addComponent(jLabelPrecioT, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(labelPrecioTercError, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(precioT, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addComponent(labelNombreError, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(labelPrecio))
                        .addGap(44, 44, 44)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtIdServicio, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtNombre)
                                .addGap(33, 33, 33))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelPrecioError, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(BtnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(BtnLimpiar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(BtnCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtIdServicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelNombreError)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPrecio))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelPrecioError)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(cbTipoServicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(RBlocal)
                    .addComponent(labelLocalTercError))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(RBter)
                    .addComponent(precioT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPrecioT)
                    .addComponent(jLabelPrecioT))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
                .addComponent(labelPrecioTercError)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BtnAceptar)
                    .addComponent(BtnLimpiar)
                    .addComponent(BtnCerrar))
                .addGap(26, 26, 26))
        );

        cbTipoServicio.addItem(TipoServicio.Planchado.toString());
        cbTipoServicio.addItem(TipoServicio.Valets.toString());
        cbTipoServicio.addItem(TipoServicio.Otros.toString());

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BtnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnLimpiarActionPerformed
        txtNombre.setText(null);
        txtPrecio.setText(null);
        precioT.setText(null);
        this.RBter.setSelected(false);
        this.RBlocal.setSelected(false);
    }//GEN-LAST:event_BtnLimpiarActionPerformed

    private void BtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnAceptarActionPerformed
        // Aceptar
        String nombre;
        
        try{
            nombre = txtNombre.getText();
            if(Validacion.soloNumeros(nombre)){
                JOptionPane.showMessageDialog(null, "No puede ingresar un nombre compuesto solo por números. Seleccione otro nombre por favor.", "Nombre", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
        }catch (Exception e){
            this.labelNombreError.setVisible(true);
            return;
        }
        
        Double pre=null;
        if((!RBlocal.isSelected() & !this.RBter.isSelected())){
            this.labelLocalTercError.setVisible(true);
            this.labelPrecioError.setVisible(true);
            return;
        }
        if(Validacion.preciosValidos(txtPrecio.getText())){
            pre=new Double(txtPrecio.getText());
        }
        else{
            this.labelLocalTercError.setVisible(true);
            this.labelPrecioError.setVisible(true);
            return;
        }

        String tipo=this.cbTipoServicio.getSelectedItem().toString();

        Double preT;
        boolean esTercerizado = false;
        if (RBlocal.isSelected() == true) {
            preT = new Double("0.0");
        } else {
            
            if(Validacion.preciosValidos(precioT.getText())){
                 preT = new Double(precioT.getText());
                esTercerizado = true;
            }else{
                this.labelPrecioTercError.setVisible(true);
                this.jLabelPrecioT.setVisible(true);
                return;
            }
        }

        AdministradorServicio admServicio = null;
        try {
            admServicio = new AdministradorServicio();
            System.out.println("");
            if(!admServicio.nombreDeServicioTomado(nombre,servicio.getCodServicio())){
                admServicio.modificarServicio(servicio.getCodServicio(), nombre,pre,tipo, esTercerizado,preT);
            }else{
                JOptionPane.showMessageDialog(null,"Ya existe un servicio con ese nombre", "Nombre Existente",JOptionPane.INFORMATION_MESSAGE);
                return;
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null,"No se pudieron guardar los cambios", "Fallo",JOptionPane.ERROR_MESSAGE);
        }
        this.dispose();
    }//GEN-LAST:event_BtnAceptarActionPerformed

    private void RBterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RBterActionPerformed
        if(!jLabelPrecioT.isVisible())
            jLabelPrecioT.setVisible(true);
        precioT.setVisible(true);
    }//GEN-LAST:event_RBterActionPerformed

    private void precioTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_precioTActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_precioTActionPerformed

    private void txtPrecioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPrecioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPrecioActionPerformed

    private void RBlocalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RBlocalActionPerformed
        if(jLabelPrecioT.isVisible())
            jLabelPrecioT.setVisible(false);
        if(this.labelPrecioTercError.isVisible())
            labelPrecioTercError.setVisible(false);
        precioT.setVisible(false);
    }//GEN-LAST:event_RBlocalActionPerformed

    private void txtIdServicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIdServicioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtIdServicioActionPerformed

    private void txtNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNombreActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNombreActionPerformed

    private void BtnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCerrarActionPerformed
        BuscarServicios buscarServicios = new BuscarServicios(null, rootPaneCheckingEnabled);
        buscarServicios.setVisible(false);
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_BtnCerrarActionPerformed

    private void cbTipoServicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbTipoServicioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbTipoServicioActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnAceptar;
    private javax.swing.JButton BtnCerrar;
    private javax.swing.JButton BtnLimpiar;
    private javax.swing.JRadioButton RBlocal;
    private javax.swing.JRadioButton RBter;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cbTipoServicio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabelPrecioT;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel labelLocalTercError;
    private javax.swing.JLabel labelNombreError;
    private javax.swing.JLabel labelPrecio;
    private javax.swing.JLabel labelPrecioError;
    private javax.swing.JLabel labelPrecioError1;
    private javax.swing.JLabel labelPrecioT;
    private javax.swing.JLabel labelPrecioTercError;
    private javax.swing.JTextField precioT;
    private javax.swing.JTextField txtIdServicio;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtPrecio;
    // End of variables declaration//GEN-END:variables

    private void cargarServicio() {
        this.labelPrecioT.setVisible(false);
        this.txtIdServicio.setText(this.servicio.getCodServicio().toString());
        this.txtNombre.setText(this.servicio.getNombre());
        this.txtPrecio.setText(this.servicio.getPrecio().toString());
        this.cbTipoServicio.setSelectedItem(this.servicio.getTipoServicio());
        if(this.servicio.esTercerizado()){
            this.RBter.setSelected(true);
            this.precioT.setVisible(true);
            this.precioT.setText(this.servicio.getPrecioProveedor().toString());
            if(!jLabelPrecioT.isVisible())
                this.jLabelPrecioT.setVisible(true);
        }
        else{
            this.RBlocal.setSelected(true);
            this.precioT.setVisible(false);
            if(jLabelPrecioT.isVisible())
                this.jLabelPrecioT.setVisible(false);
        }
    }
}
